#include <iostream>
#include <string>
#include "httplib/httplib.h"
#include "searcher.hpp"
#include "log.hpp"

const std::string root_path = "./wwwroot";
const std::string text_path = "data/output/raw_html.txt"; 


int main()
{
    ns_search::Searcher* searcher = new ns_search::Searcher();
    searcher->InitSearcher(text_path);

    httplib::Server svr;
    svr.set_base_dir(root_path);
    LOG(NORMAL, "服务器已启动...");

    svr.Get("/s", [&searcher](const httplib::Request& req, httplib::Response& resp){
        // for test
        // resp.set_content("你好,世界!", "text/plain; charset=utf-8");
        if(!req.has_param("word"))
        {
            resp.set_content("搜索词不能为空!", "text/plain; charset=utf-8");
            return;
        }
        // 提取参数
        std::string word = req.get_param_value("word");
        // std::cout << "用户查找的是: " << word << std::endl;
        LOG(NORMAL, "用户查找的是: " + word);
        std::string json_str;
        searcher->search(word, &json_str);
        // std::cout << "搜索结果为：" << json_str << std::endl;
        resp.set_content(json_str, "application/json");
    });
    // 绑定任意ip
    svr.listen("0.0.0.0", 8081);
    return 0;
}

